/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.twodstar.photobattle;

import java.util.Arrays;
import java.util.List;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.twodstar.photobattle.R;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment_Login extends Fragment 
{
	private static Fragment_Login m_Inst = null;
	
	private Button loginButton;
	private Dialog progressDialog;
	
	
	public static Fragment_Login Instance()
	{
		if(m_Inst == null)
			m_Inst = new Fragment_Login();
			
		return m_Inst;
	}
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.login, container, false);
        
        loginButton = (Button) view.findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				onLoginButtonClicked();
			}
		});

        return view;
    }
    
    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
    	Log.i("game", "test frag act");
		super.onActivityResult(requestCode, resultCode, data);
		ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);
	}

	private void onLoginButtonClicked() 
	{
		progressDialog = ProgressDialog.show(
				getActivity(), "", "Logging in...", true);
		
		List<String> permissions = Arrays.asList(
													"basic_info", 
													"user_about_me",			
													"user_photos",				
													"friends_photos"																										
													);		
	
		
		ParseFacebookUtils.logIn(permissions, getActivity(), new LogInCallback() 
		{
			@Override
			public void done(ParseUser user, ParseException err) 
			{
				progressDialog.dismiss();
				if (user == null) 
				{
					Log.i("game","Uh oh. The user cancelled the Facebook login.");
				} 
				else if (user.isNew()) 
				{
					Log.i("game","User signed up and logged in through Facebook!");
					ShowFragmentCreateNewBattle();
				} 
				else 
				{
					Log.i("game","User logged in through Facebook! " + user.get("FBID"));
					ShowFragmentCreateNewBattle();
					
				}
			}
		});
	}

	private void ShowFragmentCreateNewBattle() 
	{			
		MainActivity Act = (MainActivity)getActivity();
		if(Act != null)
			Act.ShowSelection();
		
	}

    
}
